package my.menu.menuImp.AdminChildMenus;

import my.dao.daoImpl.BookDao;
import my.dao.daoImpl.BookTypeDao;
import my.support.BookViewer;
import my.menu.Menu;
import my.vo.voImpl.Book;
import my.vo.voImpl.BookType;
import my.vo.voImpl.User;

import java.util.List;

/**
 * @author 星环
 * @date 2022/11/1 9:04:11
 * @description 重复的内容见 {@link AdminBookMenu}
 */
public class AdminBookTypeMenu extends Menu {

    BookTypeDao bookTypeDao = BookTypeDao.getInstance();
    BookDao bookDao = BookDao.getInstance();

    BookViewer bookViewer;
    public AdminBookTypeMenu(User user){
        super(  List.of("类别一览","添加新类","据id更新","按类名查找图书","删除分类"),
                List.of("showAllBookTypes","addNewType","updateNameById","showBooksByBookTypeName","deleteBookTypeById")
        );
        setUser(user);
        bookViewer  = new BookViewer(user);
    }

    public boolean showAllBookTypes(){
        System.out.println("showAllBookTypes");
        bookTypeDao.getAll().forEach(System.out::println);
        return true;
    }

    public boolean addNewType(){
        System.out.print("请输入新类别的名称：");
        String name = sc.next();

        bookTypeDao.addNewVo(new BookType(name));
        System.out.println("添加成功");
        return true;
    }

    public boolean updateNameById(){
        System.out.println("请输入要修改的类别的id");
        int id = sc.nextInt();

        if(!srchByIdAndShow(id)){
            return true;
        }

        updateNameByIdChecked(id);

        return true;
    }

    private void updateNameByIdChecked(int id) {
        System.out.println("请输入您要修改的新名称");
        String name = sc.next();

        BookType newBookType = new BookType();
        newBookType.setId(id);
        newBookType.setName(name);

        bookTypeDao.updateById(newBookType);
    }


    public boolean showBooksByBookTypeName(){
        System.out.println("请输入要查询的分类名");
        String btName = sc.next();

        bookViewer.showBooksByBookTypeName(btName);

        return true;
    }

    public boolean deleteBookTypeById(){
        bookViewer.showTypeOptionList();
        System.out.println("请输入要删除的分类id");
        int id = sc.nextInt();

        if (!srchBookTypeById(id)) {
            return true;
        }

        System.out.println("是否删除此分类下的图书？是1否2");
        int n = sc.nextInt();

        //先处理要删的书
        if (n == 1) {
            List<Book> books = bookDao.deleteByTypeId(id);
            if(books.size() != 0){
                System.out.println("已经删除如下图书");
                bookViewer.formatAndShowBooks(books);
            }
        } else {
            resetBookIdsOption(id);
        }
        bookTypeDao.deleteById(id);
        System.out.println("删除成功！");
        return true;
    }

    private boolean srchBookTypeById(int id) {
        BookType bookType = bookTypeDao.getById(id);
        return bookType != null;
    }


    /**
     * 给每个因为删除书类而没有类别的书设置分类
     * @param id
     */
    private void resetBookIdsByTypeId(int id) {
        List<Book> books = bookDao.getBooksByTypeId(id);
        int total = books.size();
        int cnt = 1;
        for(Book b : books) {
            boolean rightTypeId = false;
            while (!rightTypeId){
            System.out.println("--当前第 " + cnt + " / " + total + "本书--");
                System.out.println("当前图书："  + b);
                bookViewer.showTypeOptionList();
                System.out.println("请输入新分类id");
                int newId = sc.nextInt();

                if (!(newId == id) && srchBookTypeById(newId)) {
                    rightTypeId = true;

                    bookDao.updateTypeIdById(b.getId(),newId);
                }else {
                    System.out.println("您输入的类别编号不存在或将被删除,请重新输入");
                }
            }
            cnt++;
        }
        System.out.println();
    }

    private void resetBookIdsOption(int id){
        System.out.println("是否要挨个设置图书的分类？1是2否");
        int settype = sc.nextInt();
        if (settype == 1) {
            resetBookIdsByTypeId(id);
        } else {
            resetBookTypeIdOnce(id);
        }
    }
    private void resetBookTypeIdOnce(int oldid){
        boolean rightTypeId = false;

        while (!rightTypeId){
            bookViewer.showTypeOptionList();
            System.out.println("请输入新的类别id：");
            int newId = sc.nextInt();

            if(newId != oldid && srchBookTypeById(newId)){
                rightTypeId = true;
                System.out.println("新的类别是" + bookTypeDao.getNameById(newId));

                List<Book> books = bookDao.updateTypeId(oldid, newId);

                bookViewer.formatAndShowBooks(books);
            }else {
                System.out.println("您输入的分类不存在或将被删除");
            }
        }
    }

    private boolean srchByIdAndShow(int id) {
        BookType bookType = bookTypeDao.getById(id);

        if(bookType == null){
            System.out.println("没有找到这个分类");
            return false;
        }
        System.out.println("找到如下");
        System.out.println(bookType);
        return true;
    }
}